package org.webArchi.common.web.action.usersetup;

import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.apache.struts2.interceptor.PrincipalAware;
import org.apache.struts2.interceptor.PrincipalProxy;
import org.apache.struts2.interceptor.SessionAware;
import org.dom4j.rule.Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.webArchi.common.api.domain.User;
import org.webArchi.common.api.service.UserManagmentService;

import java.util.Map;

/**
 * @author  chamith malinda
 */
public class UserSetupAction extends ActionSupport implements SessionAware, PrincipalAware {
    private static final Logger logger = LoggerFactory.getLogger(UserSetupAction.class);

    private UserManagmentService  userManagmentService;

    public UserSetupAction(UserManagmentService  userManagmentService) {
        this.userManagmentService=userManagmentService;
    }

    private String userName;
    private String password;
    private String phoneNumber;
    private String nic;
    private String email;
    private String code;

    /**
     * Populating new user object for persisting new user
     */
    private User populateUserObject(){
        logger.debug("Attempt to populate new user object ");
        User user=new User();
        user.setNic(nic);
        return  user;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getNic() {
        return nic;
    }

    public void setNic(String nic) {
        this.nic = nic;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    /**
     *
     * Load the User Setup Page
     * @return success or error
     */
    public String userSetupPage(){
       return ActionSupport.SUCCESS;
    }

    /**
     *
     * Setup a new user
     * @return success or error
     */
    public String setupNewUser(){

        boolean isAuth;
        String actionType = "";

        try{


         isAuth = isAuthenticate();
         logger.info("-- New Authenticated ? "+isAuth);

         if(isAuth){

          // save the user data (service call)
         userManagmentService.createUser(populateUserObject());
          actionType = "success";
          logger.info("-- New user created -- ");

         }else{
             actionType = "error";
         }

          if(!isAuth){
           addActionError(getText("user.setup.auth.fail"));
          }else{
           addActionMessage(getText("user.setup.auth.success"));
          }

        }catch(Exception e){
          logger.debug("exception when going to create a new user: {} ", e);
          actionType = "error";
          addActionError(getText("user.setup.general.error"));
        }

        return actionType;

    }

     /**
     *
     * Validate the USER
     * @return success or error
     */
    public String validateUser(){

        boolean isValid;
        String actionType = "";

        try{

         isValid = isValidate();

         if(isValid){
          addActionMessage(getText("user.setup.valid.success")+" "+getUserName());
          actionType = "success";

         }else{
          addActionError(getText("user.setup.valid.fail"));
          actionType = "error";
         }

        }catch(Exception e){
          logger.debug("exception when going to validate the user new user: {} ", e);
          actionType = "error";
          addActionError(getText("user.setup.general.error"));
        }

        return actionType;

    }

/**
     *
     * Authenticate the USER with legacy
     * @return success or error
     */
     public boolean isAuthenticate(){

        // service call
        return true;

     }

     /**
     *
     * Validate the USER
     * @return success or error
     */
    public boolean isValidate(){

        // service call
        return true;

     }

    /**
     *
     * Load the recovery password page
     * @return success or error
     */

     public String passwordRecoveryPage(){

        return ActionSupport.SUCCESS;

     }

     public String passwordRecovery(){

       String actionType = "";

       try{

          boolean isAuth=isAuthenticate();

          if(isAuth){

           if(setAuthCode()){
              actionType = "success";
           }else{
              actionType = "error";
           }

          }else{
             actionType = "error";
          }



       }catch(Exception e){
           e.printStackTrace();
           actionType = "error";
       }

         return actionType;
     }

    public boolean setAuthCode(){

        boolean auth =false;

         try{

          // Code for sending the authentication code to the phone number or email address.

          auth = true;

         }catch(Exception e){
            e.printStackTrace();
         }

         return auth;
    }

    public String passwordRecoveryEnterCodePage(){

        return ActionSupport.SUCCESS;
    }

    public String passwordRecoveryCode(){

        String actionType = "";

        try{

         String pwdRecCode = getPasswordRecoveryCode();

         if(pwdRecCode.equals(getCode())){
           actionType = "success";
         }else{
           actionType = "error";
         }


        }catch(Exception e){
           e.printStackTrace();
           actionType = "error";

        }

        return actionType;
    }

    public String getPasswordRecoveryCode(){

      String pwdRecCode = "";

       try{

           // Code for gathering the pwd recovery code (from session or from DB)

       }catch(Exception e){
          e.printStackTrace();
       }

        return pwdRecCode;
    }

    public String passwordRecoverySave(){

        String actionType = "";

        try{

            // code for saving the new password to the database

            actionType = "success";


        }catch(Exception e){
          e.printStackTrace();
            actionType = "error";
        }

       return actionType;
    }

    public String passwordChangePage(){
        return ActionSupport.SUCCESS;
    }

    public String changePassword(){

        String actionType = "";

        try{

            // code for saving the new password to the DB

            actionType = "success";

        }catch(Exception e){
          e.printStackTrace();
            actionType = "error";
        }

        return actionType;
    }


    @Override
    public void setPrincipalProxy(PrincipalProxy principalProxy) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void setSession(Map<String, Object> stringObjectMap) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
